<?php

class MenuController {


    public function invoke() {
        $viewFile = 'block/block_menu_phu/views/menu.html';
        $xtemplate = new XTemplate($viewFile);

        $menu = $this->showHorizontalMenu(212, 0);

        $xtemplate->assign('MENU', $menu);
        $xtemplate->parse('main');
        $xtemplate->out('main');
    }

    private function getMenuListByCategoryID($categoryID, $parentID = 0) {
        try {
            $db = new Database();
            $menu = new Menu();
            $menu->setCategoryID($categoryID);
            $menu->setParentID($parentID);
            $result = $menu->getListByParentIDAndCategoryID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function showHorizontalMenu($categoryID, $parentID = 0) {
        $menuString = '';
        $result = $this->getMenuListByCategoryID($categoryID, $parentID);

        if ($result != null) {
            $menuString .='';
            for ($i = 0; $i < count($result); $i++) {
                if ($result[$i]->getStatus() == 1) {
                    $menuString .='<li><a target="'.$result[$i]->getTarget().'" href = "' . $result[$i]->getLink() . '">' . $result[$i]->getTitle() . '</a>';
                    $menuString .= $this->showHorizontalMenu($categoryID, $result[$i]->getID(), $menuString);
                    $menuString .='</li>';
                }
            }
        }
        return $menuString;
    }

}

?>
